import { createRouter, createWebHistory } from 'vue-router'
import useUserStore from '@/store/modules/user'
import login from '../views/login.vue'
import layout from '../views/layout.vue'
import index from '../views/index.vue'
import category from '../views/category/index.vue'
import comment from '../views/comment/index.vue'
import favorite from '../views/favorite/index.vue'
import link from '../views/link/index.vue'
import resource from '../views/resource/index.vue'
import tag from '../views/tag/index.vue'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/layout' // 默认访问根路径时重定向到登录页
    },
    {
      path: '/login',
      name: 'login',
      component: login,
    },
    {
      path: '/layout',
      name: 'layout',
      component: layout,
      children: [
        {
          path: 'index',
          name: 'index',
          component: index
        },
        {
          path: 'category',
          name: 'category',
          component: category
        },
        {
          path: 'comment',
          name: 'comment',
          component: comment
        },
        {
          path: 'favorite',
          name: 'favorite',
          component: favorite
        },
        {
          path: 'link',
          name: 'link',
          component: link
        },
        {
          path: 'resource',
          name: 'resource',
          component: resource
        },
        {
          path: 'tag',
          name: 'tag',
          component: tag
        },
      ]
    },
  ],
})
// router.beforeEach((to, from, next) => {
//   // 在守卫内部使用 store
//   const userStore = useUserStore()
  
//   const whiteList = ['/login']
//   console.log(userStore.userId);  
//   console.log('当前用户ID:', userStore.userId)
//   console.log('是否在白名单:', whiteList.includes(to.path))
//   console.log('是否需要重定向到登录页:', !whiteList.includes(to.path) && userStore.userId === '')
  
//   if (!whiteList.includes(to.path) && userStore.userId === '') {
//     next('/login')
//   } else {
//     next()
//   }
// })
export default router
